<?php
/**
 * User: Netman
 * Date: 14-3-27
 * Time: 下午9:53
 */

class Stourweb_Controller extends Class_Controller {

    /*
     * before
     */
    public function before()
    {
        if(!ISADMIN)exit;
        parent::before();
        $this->assign('coreVersion',Model_SystemParts::getCoreMajorVersion());
        $params = $this->request->param('params');
        $sysconfig = ORM::factory('sysconfig')->getConfig(0);
        //未设置默认有效期为2h
        $hours = intval($sysconfig['cfg_admin_login_time_limit'])>0 ? intval($sysconfig['cfg_admin_login_time_limit']) : 2;
        $this->assign("sysconfig", $sysconfig);

        $this->params = $this->analyze_param($params);
        $_GET=array_merge($this->params,$_GET);
        $controller = $this->request->controller();
        $action = $this->request->action();
        $second_action=$this->params['action'];
        if($controller != 'login')
        {
            $needCheck=1;//uplodify检测

            if($needCheck)
            {
                $cookie_username = Cookie::get('username');
                if($this->request->is_ajax() && Common::checkLogin($cookie_username))
                    return;

                $session = Session::instance();
                $session_username = $session->get('username');
                $uploadcookie = Arr::get($_POST,'uploadcookie');

                $serectkey = !empty($cookie_username) ? $cookie_username : $session_username;
                $serectkey = !empty($serectkey) ? $serectkey : $uploadcookie;
                if(isset($serectkey))
                {
                    $lifetime = Cookie::get('lifetime');
                    if($lifetime && time()>intval($lifetime) &&  empty($_COOKIE['lifetime']))
                    {

                        $session = Session::instance();
                        $session->delete('username');
                        $session->delete('userid');
                        $session->delete('roleid');
                        Cookie::delete('lifetime');
                        Cookie::delete('username');
                        exit("<script>top.location.href='login';</script>");
                    }
                    $result = Common::checkLogin($serectkey);
                    if(!$result)
                    {
                        $session = Session::instance();
                        $session->delete('username');
                        $session->delete('userid');
                        $session->delete('roleid');
                        Cookie::delete('lifetime');
                        Cookie::delete('username');
                        $this->request->redirect('login/index');
                    }
                    else
                    {
                        $session = Session::instance();
                        $serectkey = Common::authcode($result['username'].'||'.$result['password'],'');
                        $session->set('username',$serectkey);
                        $session->set('userid',$result['id']);
                        $session->set('roleid',$result['roleid']);
                        Cookie::set('lifetime',time()+$hours * 3600,$hours * 3600);
                        Cookie::set('username', $serectkey,$hours * 3600);
                        $rolemodule=ORM::factory('role_module')->where("roleid='{$result[0]['roleid']}'")->as_array();
                        $session->set('rolemodule',$rolemodule);
                    }
                }
                else
                {
                    $session = Session::instance();
                    $session->delete('username');
                    $session->delete('userid');
                    $session->delete('roleid');
                    Cookie::delete('lifetime');
                    Cookie::delete('username');
                    $this->request->redirect('login/index');
                }
                $web_host = Common::get_main_host();
                $this->assign('web_host','/newtravel/price');
                Common::addLog($controller,$action,$second_action);

            }

        }

    }

    public function after()
    {
      /*  ProfilerToolbar::addData('test string');
        ProfilerToolbar::addData(rand(1, 1000)/ rand(1, 1000));
        ProfilerToolbar::addData($this->request->headers());
        ProfilerToolbar::addData('other data','second tab');
        ProfilerToolbar::firebug();*/
    }
    /*
 * 显示模板
 * @param string $tpl,模板名
 * */
    public function display($tpl)
    {
        $view = Stourweb_View::factory($tpl);

        foreach($this->_data as $key=>$value)
        {
            $view->set($key,$value);
        }

        $this->response->body($view->render());


    }
}